<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Contacts_Model extends CI_Model{
	protected $_name = "contacts";
	protected $_primary = "ConId";
	var $FullName = "FullName", $Email = "Email", $Phone = "Phone", $Title = "Title", $Detail = "Detail", $Date = "Date", $Reply = "Reply", $Status = "Status";
	
	function getContacts($Status){
		$this->db->where($this->Status, $Status);
		$this->db->order_by($this->Date, 'ASC');
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function getContactsByDate($fromDate, $toDate, $Status){
		$d1 = explode('-', $fromDate);
    	$fromDate = date('Y-m-d', mktime(0,0,0,$d1[1],$d1[0],$d1[2]));
		$d2 = explode('-', $toDate);
    	$toDate = date('Y-m-d', mktime(0,0,0,$d2[1],$d2[0],$d2[2]));
		$this->db->where("DATE(".$this->Date.") >=", $fromDate);
		$this->db->where("DATE(".$this->Date.") <=", $toDate);
		$this->db->where($this->Status, $Status);
		$this->db->order_by($this->Date, 'ASC');
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function getMinDateOfContact(){
		$this->db->select('Date');
		$this->db->order_by($this->Date, 'ASC');
		$this->db->limit(1);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$strtotime = strtotime($query->row()->Date);
	        $date = date('d-m-Y',$strtotime);
			return $date;
		}
		return false;
	}
	
	function countContact($Status){
		$this->db->where($this->Status, $Status);
		$query = $this->db->get($this->_name);
		return $query->num_rows();
	}
	
	function getContactsLimit($Status, $currentPage, $number){
		$this->db->where($this->Status, $Status);
		$this->db->limit($number, $currentPage);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = $currentPage;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function searchContacts($keysearch, $fieldsearch, $Status){
		switch($fieldsearch){
			case 1:
				$this->db->like($this->Title, $keysearch);	
				break;
			case 2:
				$this->db->like($this->FullName, $keysearch);	
				break;
			default:
				$this->db->like($this->Title, $keysearch);	
				break;
		}
		$this->db->where($this->Status, $Status);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function deleteContact($ConId){
		try{
			$this->db->where($this->_primary, $ConId);
			$this->db->update($this->_name, array($this->Status => 3));
			return true;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function getContactByConId($ConId){
		$this->db->where($this->Status." <>", 3);
		$this->db->where($this->_primary, $ConId);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			return $query->row();
		}
		return false;
	}
	
	function updateContact($ConId){
		try{
			$this->db->where($this->_primary, $ConId);
			$data = array(
				$this->Reply => trim($this->input->post($this->Reply, true)),
				$this->Status => 2
			);
			$this->db->update($this->_name, $data);
			return true;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function insertContact(){
		$this->db->set($this->FullName, trim($this->input->post('fullname', true)));
		$this->db->set($this->Email, trim($this->input->post('email', true)));
		$this->db->set($this->Phone, trim($this->input->post('phone',true)));
		$this->db->set($this->Title, trim($this->input->post('title',true)));
		$this->db->set($this->Detail, trim($this->input->post('detail',true)));
		$this->db->set($this->Date, date('Y-m-d H:i:s'));
		$this->db->set($this->Status, 1);
		$this->db->insert($this->_name);
		$id = $this->db->insert_id();
		return $id;
	}
}